package com.duoduo.common.api.help;


import com.duoduo.common.api.constants.Constant;
import com.duoduo.common.api.domain.DelayExecuteMessage;
import com.duoduo.common.api.myinterface.delay.DelayJvmManage;
import com.duoduo.common.api.spi.extension.ExtensionLoader;

import java.io.Serializable;

/**基于jvm的延迟任务
 * @author duoduo
 *
 */
public class JvmDelayMessageInnerUtil {

    private static String delayJvmType = SpringContextInnerUtil.get(Constant.DELAY_JVM_MNGTYPE);
    private static DelayJvmManage delayManage = ExtensionLoader.getExtensionLoader(DelayJvmManage.class).getExtension(delayJvmType);

    /**放入任务
	 * @param message: 任务信息
	 * @param delayTime: 指定多少秒后执行
	 * @return 返回唯一id，用于取消任务，如果message里有traceId就直接返回
	 */
    public static <T extends Serializable> long sendDelayMessage(DelayExecuteMessage<T> message, int delayTime, Runnable handler) {
        return delayManage.sendDelayMessage(message, delayTime, handler);
    }
    
    /**取消延迟任务
	 * @param traceId 唯一id
	 * @return 成功true；失败false
	 */
    public static boolean cancelTask(long traceId) {
    	return delayManage.cancelTask(traceId);
    }

}
